<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of bpServer
 *
 * @author QuangKhoi
 */
class bpServer {
    /*
     * @param soap_server
     */

    public $server;

    public function __construct($url) {
        $this->server = new soap_server();
        $this->server->configureWSDL($url);
        $this->server->decode_utf8 = false;
        $this->server->soap_defencoding = "UTF-8";
        $this->complexType();
        $this->registerProc("login", array("login" => "tns:login"), array("return" => "tns:userinfo"));
        $this->registerProc("saveBret", array("id" => "xsd:int", "userid" => "xsd:int", "name" => "xsd:string", "description" => "xsd:string", "alias" => "xsd:string", "folderid" => "xsd:int", "data" => "xsd:string"), array("return" => "xsd:string"));
        $this->registerProc("getBretData", array("id" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("getBrets", array("userid" => "xsd:int"), array("return" => "xsd:string"));
        $this->registerProc("getMostSharedBrets", array("userid" => "xsd:int", "limit" => "xsd:int"), array("return" => "xsd:string"));
    }

    public function complexType() {
        $this->server->wsdl->addComplexType("ArrayOfString", "complexType", "array", "", "SOAP-ENC:Array", array(), array(array("ref" => "SOAP-ENC:arrayType", "wsdl:arrayType" => "xsd:string[]")), "xsd:string");
        $this->server->wsdl->addComplexType("spikes", "complexType", "array", "", "SOAP-ENC:Array", array(), array(array("ref" => "SOAP-ENC:arrayType", "wsdl:arrayType" => "tns:spike[]")), "tns:spike");

        //ComplexType for login
        $this->server->wsdl->addComplexType('login', 'complexType', 'struct', 'login', '', array(
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string')));
        //ComplexType for UserInfo
        $this->server->wsdl->addComplexType('userinfo', 'complexType', 'struct', 'userinfo', '', array(
            'id' => array('name' => 'email', 'type' => 'xsd:string'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string'),
            'name' => array('name' => 'name', 'type' => 'xsd:string'),
            'company' => array('name' => 'company', 'type' => 'xsd:string'),
            'address' => array('name' => 'address', 'type' => 'xsd:string'),
            'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
            'position' => array('name' => 'position', 'type' => 'xsd:string'),
            'dateregistered' => array('name' => 'dateregistered', 'type' => 'xsd:date'),
            'token' => array('name' => 'token', 'type' => 'xsd:string'),
            'type' => array('name' => 'type', 'type' => 'xsd:int')));

        //ComplexType for User
        $this->server->wsdl->addComplexType('user', 'complexType', 'struct', 'user', '', array(
            'userid' => array('name' => 'userid', 'type' => 'xsd:int'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'password' => array('name' => 'password', 'type' => 'xsd:string'),
            'name' => array('name' => 'name', 'type' => 'xsd:string'),
            'company' => array('name' => 'company', 'type' => 'xsd:string'),
            'address' => array('name' => 'address', 'type' => 'xsd:string'),
            'phone' => array('name' => 'phone', 'type' => 'xsd:string'),
            'position' => array('name' => 'position', 'type' => 'xsd:string'),
            'dateregistered' => array('name' => 'dateregistered', 'type' => 'xsd:date'),
            'token' => array('name' => 'token', 'type' => 'xsd:string'),
            'timezone' => array('name' => 'timezone', 'type' => 'xsd:int')));
    }

    public function registerProc($procedure, $params, $returns, $style = "rpc", $use = "encoded", $description = "", $namespace = false, $action = false) {
        $this->server->register($procedure,
                // parameter list:
                $params,
                // return value(s):
                $returns,
                // namespace:
                $namespace,
                // soapaction: (use default)
                $action,
                // style: rpc or document
                $style,
                // use: encoded or literal
                $use,
                // description: documentation for the method
                $description);
        //self::service($http_response_header);
    }

    /*
     * SOAP Procedures
     */

    public function service($data) {
        $this->server->service($data);
    }
    
    public static function login($login) {
        return User::login($login);
    }

    public static function saveBret($id, $userid, $name, $description, $alias, $folderid, $data) {
        return Bret::saveBret($id, $userid, $name, $description, $alias, $folderid, $data);
    }
    
    public static function getBretData($id){
        return Bret::getBretData($id);
    }
    
    public static function getBrets($userid) {
        return Bret::getBrets($userid);
    }
    
    public static function getMostSharedBrets($userid, $limit) {
        return Bret::getMostSharedBrets($userid, $limit);
    }
}